package com.example.demo.service.impl;

import com.example.demo.entity.Person;
import com.example.demo.mapper.PersonMapper;
import com.example.demo.service.LoginService;
import com.example.demo.utils.ShiroUtils;
import lombok.extern.java.Log;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {

    @Autowired
    PersonMapper personMapper;


    /**
     * 验证用户是否存在
     * @param user_name 用户名
     * @param user_password 用户的密码
     * @return
     */
    @Override
    public Boolean loginCheck(String user_name, String user_password) {

        Person person=personMapper.findPersonByName(user_name);
        if(person!=null ){
            String db_password=ShiroUtils.md5(user_password,person.getUser_salt());
            System.out.println("解析过后的密码"+db_password +"  数据库中的 密码"+person.getUser_password());
            if(db_password.equals(person.getUser_password())){
                return  true;
            }
        }
        return false;
    }

    /**
     * 按照等级查找用户
     * @param user_lev
     * @return
     */
    @Override
    public List<Person> findall(int user_lev) {
        return personMapper.findPersonAll(user_lev);
    }

    @Override
    public Person selectPersonById(Integer id) {
        return personMapper.findPersonById(id);
    }

    @Override
    public Person selectPersonByName(String name) {
        return personMapper.findPersonByName(name);
    }

    @Override
    public Boolean addPerson(Person person) {
        int result=personMapper.addPerson(person);
        return result>0;
    }


}
